package com.kensure.controller;

import co.kensure.frame.ResultInfo;
import co.kensure.frame.ResultRowInfo;
import co.kensure.frame.ResultRowsInfo;
import co.kensure.http.RequestUtils;
import co.kensure.mem.PageInfo;
import com.alibaba.fastjson.JSONObject;
import com.kensure.shike.collect.model.SkCollect;
import com.kensure.shike.collect.service.SkCollectService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping(value = "collect")
public class SKCollectController {

    @Resource
    private SkCollectService skCollectService;

    /**
     * 后台信息列表
     *
     * @param req
     * @param rep
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "list.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public ResultInfo list(HttpServletRequest req, HttpServletResponse rep) {

        JSONObject json = RequestUtils.paramToJson(req);
        PageInfo page = JSONObject.parseObject(json.toJSONString(), PageInfo.class);
        List<SkCollect> list = skCollectService.selectByWhere(page);
        Long count = skCollectService.selectCountByWhere(null);

        return new ResultRowsInfo(list, count);
    }

    /**
     * 详情
     *
     * @param req
     * @param rep
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "get.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public ResultInfo get(HttpServletRequest req, HttpServletResponse rep) {
        JSONObject json = RequestUtils.paramToJson(req);
        Long id = json.getLong("id");
        SkCollect skCollect = skCollectService.detail(id);

        return new ResultRowInfo(skCollect);
    }

    /**
     * 内容删除
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "del.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public ResultInfo del(HttpServletRequest req, HttpServletResponse rep) {
        JSONObject json = RequestUtils.paramToJson(req);
        Long id = json.getLong("id");
        skCollectService.delete(id);
        return new ResultRowInfo();
    }

    /**
     * 前台信息保存
     *
     * @param req
     * @param rep
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "save.do", method = { RequestMethod.POST, RequestMethod.GET }, produces = "application/json;charset=UTF-8")
    public ResultInfo foo(HttpServletRequest req, HttpServletResponse rep) {

        JSONObject json = RequestUtils.paramToJson(req);

        SkCollect obj = JSONObject.parseObject(json.toString(), SkCollect.class);

        skCollectService.add(obj);
        return new ResultRowInfo();
    }
}
